﻿#include "FITKDynamicStruPostVTK.h"
#include "FITKStruPostMessageText.h"

#include <QFile>
#include <QFileInfo>

#include <vtkDataSetReader.h>

namespace Interface
{
    FITKDynamicStruPostVTK::FITKDynamicStruPostVTK()
    {
    }

    FITKDynamicStruPostVTK::~FITKDynamicStruPostVTK()
    {
    }

    StruPostType FITKDynamicStruPostVTK::getPostStructualType()
    {
        //显式分析
        return SPTDynamicExplict;
    }

    void FITKDynamicStruPostVTK::setFiles(const QStringList & files)
    {
        //文件列表
        _fileList = files;
        //对文件排序
        qSort(_fileList);
        if (_fileList.isEmpty()) return;

        this->setFileIndex(_fileList.size() - 1);

        // 初始化显示分组All。
        this->resetDisplayGroup();
    }

    void FITKDynamicStruPostVTK::nextFrame()
    {
        //当前文件索引
        const int index = this->getFileIndex();
        //正确性判断
        if ( index + 1 < 0 || index +1 >= _fileList.size() ) return;
        this->setFileIndex(index + 1);
    }

    void FITKDynamicStruPostVTK::previousFrame()
    {
        //当前文件索引
        const int index = this->getFileIndex();
        //正确性判断
        if (index - 1 < 0 || index - 1 >= _fileList.size()) return;
        this->setFileIndex(index - 1);
    }

    void FITKDynamicStruPostVTK::firstFrame()
    {
        //当前文件索引
        const int index = this->getFileIndex();
        //已经是第一个
        if (index == 0) return;
        this->setFileIndex(0);
    }

    void FITKDynamicStruPostVTK::lastFrame()
    {
        //当前文件索引
        const int index = this->getFileIndex();
        //已经是最后一个
        if (index == _fileList.size()-1) return;
        this->setFileIndex(_fileList.size() - 1);
    }

    void FITKDynamicStruPostVTK::updateText()
    {
        if (_textData && _dataSetReader) {
            QString filePath = QString(QLatin1String(_dataSetReader->GetFileName()));
            QFile file(filePath);
            QFileInfo fileInfo(file);
            QString fileName = fileInfo.fileName();
            //QString message = QString("VTK: %1  \nStep: %2  \nIncrement %3  \nStep Time = %4").arg(fileName).arg("").arg("").arg("").arg("");
            QString message = QString("VTK: %1 ").arg(fileName);
            _textData->setText(message);
        }
    }

    void FITKDynamicStruPostVTK::setFileIndex(const int index)
    {
        if (index >= _fileList.size() || index < 0) return;
        const QString f = _fileList.at(index);
        if (!QFile::exists(f)) return;
        this->setInputFile(f);
    }

    int FITKDynamicStruPostVTK::getFileIndex()
    {
        QString currentFile = this->getInputFile();
        return _fileList.indexOf(currentFile);
    }

}



